/*
Reversi.
Copyright (C) <2008>  <Vladimir Simo, Kristian Valentin, Michal Vrabel, Tibor Balazs>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once
#include <stdio.h>

//konstanty pola
const int prazdne = 0;
const int biely = 1;
const int cierny = 2;
const int maxDepth = 3;

//typy pola
const int velkost=5;
const int rozmerPola = 2*velkost; //+1 je pre navigaciu
typedef int typPole[rozmerPola][rozmerPola];
typedef typPole* smernikPole;

// suradnice
class Coordinates {
private:
	int x, y;
	Coordinates *next;
public:
	Coordinates() {next = NULL;x=y=0;};
	void freeCoordinates();
	Coordinates(int newX, int newY);
	void setX(int newX) {x = newX;};
	int getX() {return x;};
	void setY(int newY) {y = newY;};
	int getY() {return y;};
	void setNext(Coordinates *newNext) {next = newNext;};

	Coordinates *getNext() {return next;};
};

void Coordinates::freeCoordinates(){
	if (next==NULL){
		
	}
	else {
		if(next->getNext()!=NULL){
			next->~Coordinates();
		}
			delete next;
	}
}

Coordinates::Coordinates(int newX, int newY) {
	if(newX >= 0 && newY >= 0) {
		setX(newX);
		setY(newY);
	}
	next = NULL;
}


class HrPole {
private:
	typPole pole,zaloznePole;
	int playerColor;
public: 
	void init();
	smernikPole getField() {return &pole;};
	void setColor(int newColor) {playerColor = newColor;};
	int getColor() {return playerColor;};
	void printField();
	bool koniecHry();
	void nameTheWinner();
        int fieldValue(int x, int y);
        int fieldZalozne(int x, int y);
};



bool HrPole::koniecHry(){
	//skopiruj pole do zalozneho pola a skontroluj ci sa rovnaju
	//int rozne = 0;

//tato cast je zakomentovana kuoli tomu aby sa dohrala vzdy hra az
//do zaplnenia celeho pola
/*
	for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			if (pole[i][j]!=zaloznePole[i][j]){
				rozne++;
				zaloznePole[i][j]=pole[i][j];
			}
		}
	}
	//ak sa rovnaju skonci
	if (rozne==0){
		return true;
	}
*/

        //zisti pocet ciernych
        int black_count = 0;
        for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			if (pole[i][j]==cierny){
				black_count++;
			}
		}
	}

        //ziadna cierna biely vyhral koniec hry
        if (black_count == 0) {
                return true;
        }

        //zisti pocet bielych
        int white_count = 0;
        for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			if (pole[i][j]==biely){
				white_count++;
			}
		}
	}

        //ziadna biela cierny vyhral koniec hry
        if (white_count == 0) {
                return true;
        }

        //pripad, ze vsetky policka sa zaplnia
	for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			if (pole[i][j]==prazdne){
				return false;
			}
		}
	}

        return true;
}

void HrPole::init(){
	for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			pole[i][j] = prazdne;
			zaloznePole[i][j] = prazdne;
		}
	}
	pole[rozmerPola/2-1][rozmerPola/2-1] = biely;
	pole[rozmerPola/2][rozmerPola/2] = biely;
	pole[rozmerPola/2][rozmerPola/2-1] = cierny;
	pole[rozmerPola/2-1][rozmerPola/2] = cierny;
}

void HrPole::printField(){
	for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			printf("%i",pole[i][j]);
		}
		printf("%s","\n");
	}
	printf("%s","\n");
}

void HrPole::nameTheWinner(){
	int white=0,black=0;
	for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			if (pole[i][j]==biely){
				white++;
			}
			if (pole[i][j]==cierny){
				black++;
			}
		}
	}
	if (white>black){
		printf("%s%i%s%i","!!Biely vyhral!!\n",white,": ",black);
	}
	if (white<black){
		printf("%s%i%s%i","!!Cierny vyhral!!\n",white,": ",black);
	}
	if (white==black){
		printf("%s%i%s%i","!!Remiza!!\n",white,": ",black);
	}

}

int HrPole::fieldValue(int x, int y) {
        return pole[x][y];
}

int HrPole::fieldZalozne(int x, int y) {
        return zaloznePole[x][y];
}

